<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class NoticeModel{ 
    protected $CI;
    public $pageSize = 10;
    public function __construct(){
        $this->CI =& get_instance();
        $this->CI->load->database();
    } 
    /**
     * [addNotice description]
     * @return [type] [description]
     */
    public function addNotice($userId,$type,$releaseId,$title){
        $data['user_id'] =$userId;
        $data['type'] =$type;
        $data['title'] =$title;
        $data['release_id'] =$releaseId;
        $data['create_time'] =date("Y-m-d H:i:s");
        $logRet = $this->CI->db->insert('notice', $data);
        return 0;
    }


    /**
     * [userNoticeList description]
     * @return [type] [description]
     */
    public function userNoticeList($userId,$data){
        $column = "notice.release_id as releaseId,notice.type
            ,max(concat(notice.create_time,notice.title)) as title
            ,max(notice.create_time) as createTime
            ,max(concat(notice.create_time,notice.status)) as status

            ,task.name as taskname,task.cover,task.close_date as closeDate
            ,task.close_time as clsoeTime,task.id as taskId";
        $fileterArray['notice.user_id'] = $userId;
        //是否过滤类型
        if(isset($data['type']) && $data['type']){
            $fileterArray['notice.type'] = $data['type'];
        }
        //是否过滤状态
        if(isset($data['status']) && $data['status']!=''){
            $fileterArray['notice.status'] = $data['status'];
        }
        //翻页
        if(isset($data['createTime']) &&$data['createTime']){
            $fileterArray['notice.create_time<'] = $data['createTime'];
        }
         if(!empty($data['pageSize'])){
            $pageSize = $data['pageSize'];
        }
        else{
            $pageSize = $this->pageSize;
        }
        // var_dump($fileterArray);exit;
        $query = $this->CI->db->select($column)
            ->join("task_obtain as obtain","obtain.id = notice.release_id","left")
            ->join("task","task.id = obtain.task_id","left")
            ->order_by('notice.create_time', 'desc')
            ->group_by('notice.release_id,notice.type')
            ->get_where('notice', $fileterArray, $pageSize, 0);
        $result = $query->result(); 

        $ret['data'] = $result;
        
        return $ret;
    }

    /**
     * [userSecondNoticeList description]
     * @return [type] [description]
     */
    public function userSecondNoticeList($userId,$data){
        //1查询数据
        $column = "notice.id,notice.release_id as releaseId,notice.type
            ,notice.title,notice.create_time,notice.status";
        $fileterArray['notice.user_id'] = $userId;
        $fileterArray['notice.type'] = $data['type'];
        $fileterArray['notice.release_id'] = $data['releaseId'];
       
        // var_dump($fileterArray);exit;
        $query = $this->CI->db->select($column)
            ->order_by('notice.create_time', 'asc')
            ->get_where('notice', $fileterArray);
        $result = $query->result(); 

        //2置为已读
        $status = $this->CI->db->set("status",1)->where($fileterArray)->update('notice');

        $ret['data'] = $result;
        
        return $ret;
    } 

    /**
     * [userUnReadNoticeCount description]
     * @return [type] [description]
     */
    public function userNoticeCount($userId,$data){
        //1查询数据
        $column = "count(id) as total";
        $fileterArray['user_id'] = $userId;
        if($data['status']!='')
            $fileterArray['status'] = $data['status'];
        if($data['type']!='')
            $fileterArray['type'] = $data['type'];
        
       
        // var_dump($fileterArray);exit;
        $query = $this->CI->db->select($column)
            ->get_where('notice', $fileterArray,1,0);
        $result = $query->row(); 
        if($result)
            return $result->total;
        else
            return 0;
    }

}